package cn.bluethink.eguan.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bluethink.eguan.core.entity.EgActionTypeEntity;
import cn.bluethink.eguan.core.entity.EgOtactionEntity;
import cn.bluethink.eguan.core.entity.EgOtypeEntity;
import cn.bluethink.eguan.core.entity.EgPurviewEntity;
import cn.bluethink.eguan.core.entity.EgRoleEntity;
import cn.bluethink.eguan.core.mapper.EgActionTypeMapper;
import cn.bluethink.eguan.core.mapper.EgOtactionMapper;
import cn.bluethink.eguan.core.mapper.EgOtypeMapper;
import cn.bluethink.eguan.core.mapper.EgPurviewMapper;
import cn.bluethink.eguan.core.mapper.EgRoleMapper;
import cn.bluethink.eguan.model.core.EgConfigInfo;
/**
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.2.0
 * @brief 驿馆+（EGUAN）
 * @note 修订历史：
 * 1.wanglei于2018/11/09添加用户操作对象的动作类型初始化
 * 2.wanglei于2018/11/13添加对象类型的动作初始化
 */
@Service
public class EgConfigInfoService {
    
	@Autowired
	private EgConfigInfo configInfo;

    @Autowired
    private EgOtypeMapper otypeMapper;

    @Autowired
    private EgRoleMapper roleMapper;

    @Autowired
    private EgPurviewMapper purviewMapper;
    
    @Autowired
    private EgActionTypeMapper actionTypeMapper;
    
    @Autowired
    private EgOtactionMapper otactionMapper;

    public void systemInit() throws Exception {
        systemOtypeInit();
        systemRoleInit();
        systemPurviewInit();
        systemActionTypeInit();
        systemOtactionInit();
        System.out.println("eguan+：对象类型和权限配置信息初始化成功！");
    }

    /**
     * 对象类型初始化
     * 
     * @throws Exception
     */
    private void systemOtypeInit() throws Exception {
        List<EgOtypeEntity> otypeEntitities = otypeMapper.selectAll();
        for (EgOtypeEntity otype : otypeEntitities) {
        	configInfo.addOType(otype.getOtid(), otype.getName());
        }
    }

    /**
     * 用户的角色信息初始化
     * 
     * @throws Exception
     */
    private void systemRoleInit() throws Exception {
        List<EgRoleEntity> roleEntities = roleMapper.selectAll();
        for (EgRoleEntity role : roleEntities) {
        	configInfo.addRole(role.getRid(), role.getName(), role.getRole4(), role.getPurview4(),
                    role.getRole2(), role.getPurview2(), role.getRole1(), role.getPurview1());
        }
    }

    /**
     * 用户的权限配置初始化
     * 
     * @throws Exception
     */
    private void systemPurviewInit() throws Exception {
        List<EgPurviewEntity> purviewEntities = purviewMapper.selectAll();
        for (EgPurviewEntity purview : purviewEntities) {
        	configInfo.addPurview(purview.getId(), purview.getName(), purview.getDes());
        }
    }
    
    /**
     * 用户操作对象的动作类型初始化
     * 
     * @throws Exception
     */
    private void systemActionTypeInit() throws Exception {
        List<EgActionTypeEntity> actionTypeEntitites = actionTypeMapper.selectAll();
        for(EgActionTypeEntity actionType : actionTypeEntitites) {
            configInfo.addActionType(actionType.getAtid(), actionType.getName());
        }
    }
    
    /**
     * 对象类型的动作初始化
     * 
     * @throws Exception
     */
    private void systemOtactionInit() throws Exception {
        List<EgOtactionEntity> otactionEntitites = otactionMapper.selectAll();
        for(EgOtactionEntity otaction : otactionEntitites) {
            configInfo.addOTAction(otaction.getOtype(), otaction.getAtid(), otaction.getRtype(), otaction.getActFlag());
        }
    }
}
